//
//  PlazzViewController.m
//  SoundPill
//
//  Created by 宋旭东 on 15/7/3.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "PlazzViewController.h"
#import "AppDelegate.h"
#import "search_viewController.h"
#import "search_labelController.h"
#import "userInformationViewController.h"
#import "PlazzTableView.h"
#import "AttentionTableview.h"
#import "MostNewTableview.h"
#import "Find_ContentView.h"
#import "reconmendAlertView.h"
#import "IsLogout.h"
#import "MobClick.h"
#import "JCAlertView.h"
#import "UIViewController+MMDrawerController.h"
#import "CUSFlashLabel.h"
#define kDeviceHeight [UIScreen mainScreen].bounds.size.height
#define kDeviceWidth  [UIScreen mainScreen].bounds.size.width
@interface PlazzViewController ()<UIScrollViewDelegate>
{
    float offset;
    UIView *cover;
    //loading
    UIImageView *_loadingImage;
    NSString *trackViewURL;
    BOOL isReviewed;
    
}
@property (strong, nonatomic) UIButton *plazzBtn;
@property (strong, nonatomic) UIButton *mostNewBtn;
@property (strong, nonatomic) UIButton *attentionBtn;
@property (strong, nonatomic) UIButton *reconmend;
@property (strong, nonatomic) UIScrollView *BigScroll;
@property (strong, nonatomic) PlazzTableView *plazzTableview;
@property (strong, nonatomic) Find_ContentView *contentView;
@property (strong, nonatomic) MostNewTableview *mostNewTableview;
@property (strong, nonatomic) AttentionTableview *attentionTableview;
@property (strong, nonatomic) JCAlertView *alert;
@end

@implementation PlazzViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(plazzgotoPlayer:) name:@"plazzGoToPlayer" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(plazzgoToUser:) name:@"plazzGoToUser" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutAlert) name:@"logout" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(seaechClick:) name:@"searchText" object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkMonitor:) name:@"networkMonitor" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchTopic:) name:@"searchTopic" object:nil];
        
        [self loadingAnimation];
            isReviewed = [[[NSUserDefaults standardUserDefaults] valueForKey:@"isReviewed"] boolValue];
        [self reviewApp];
    }
    return self;
}
- (void)searchTopic:(NSNotification *)notification
{
    search_labelController *searchLabelVC = [[search_labelController alloc]init];
    
    [searchLabelVC getTitle:notification.object];
    [self.navigationController pushViewController:searchLabelVC animated:YES];
}
- (void)openMicrophone{
    if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
        [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
            if (!granted) {
//                [[[UIAlertView alloc] initWithTitle:@"无法使用麦克风" message:@"请在“设置-隐私-麦克风”选项中允许符点访问你的麦克风" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
                [JCAlertView showOneButtonWithTitle:@"无法使用麦克风" Message:@"请在“设置-隐私-麦克风”选项中允许符点访问你的麦克风" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
                    
                }];
            }
        }];
    }
}
- (void)logoutAlert
{
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"uid"] !=nil) {
        [JCAlertView showTwoButtonsWithTitle:@"请登录" Message:@"你的账号已在其他设备登录，请重新登录" ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"取消" Click:^{
            //        [self plazz:_plazzBtn];
        } ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            [[AppDelegate shareDelegate] chooseRootViewController];
        }];
    }
    
    [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"uid"];
    [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"token"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)networkMonitor:(NSNotification *)notification
{
    NSNumber *status = notification.object;
    if (status.intValue == 0) {
        [JCAlertView showOneButtonWithTitle:@"无法连接" Message:@"当前设备无网络, 建议打开Wifi" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];
    }
}
#pragma mark - 
#pragma mark Controll
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [MobClick beginLogPageView:@"首页"];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"uid"] != nil) {
        [IsLogout userIsLogout:self Alert:NO];
        //        NSLog(@"%@", self.signDic);
    } else {
        
    }
    [self closeAnimation];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [MobClick endLogPageView:@"首页"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [IsLogout userIsLogout:self Alert:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor] ;
    // Do any additional setup after loading the view.
    [self openMicrophone];
    [self createNavgationBar];
    [self createTableview];
    [self visionIsMostNew];
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"uid"] == nil) {
        [self navigationReconmendButtonClick:[UIButton buttonWithType:UIButtonTypeCustom]];
    }
}
- (void)qianDaoAlert:(id)responseObject showSignBtn:(BOOL)shouSignBtn
{
    if (shouSignBtn == YES) {
        [self navigationReconmendButtonClick:nil];
    } else {
        [self navigationReconmendButtonClick:_reconmend];
    }
}
- (void)closeAlertAfterSign:(id)sender
{
//    [sender setTitle:@"已签到" forState: UIControlStateNormal];
//    sender.backgroundColor = [UIColor colorWithRed:0.592 green:0.608 blue:0.627 alpha:1.00];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self closeSignAlert];
//        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//        
//        // 动画选项设定
//        animation.duration = 0.5; // 动画持续时间
//        animation.repeatCount = 2; // 重复次数
//        animation.autoreverses = YES; // 动画结束时执行逆动画
//        
//        // 缩放倍数
//        animation.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
//        animation.toValue = [NSNumber numberWithFloat:1.3]; // 结束时的倍率
//        
//        // 添加动画
//        [_reconmend.layer addAnimation:animation forKey:@"scale-layer"];
        
        [self closeAnimation];
    });
    
}

- (void)closeAnimation
{
    CALayer *viewLayer = _reconmend.layer;
    
    // 获取当前View的位置
    
    CGPoint position = viewLayer.position;
    CGPoint x = CGPointMake(position.x + 5, position.y);
    CGPoint y = CGPointMake(position.x - 5, position.y);
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    [animation setAutoreverses:YES];
    [animation setDuration:.06];
    [animation setRepeatCount:3];
    [viewLayer addAnimation:animation forKey:nil];
}
- (void)closeSignAlert
{
    [_alert dismissWithCompletion:^{
        [self playerStop];
    }];
}
- (void)visionIsMostNew
{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
//    NSLog(@"++++++%@", currentVersion);
    NSString *url = @"https://itunes.apple.com/cn/lookup?id=991508808";
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *infoArray = [responseObject objectForKey:@"results"];
        if ([infoArray count]) {
            NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
            NSString *lastVersion = [releaseInfo objectForKey:@"version"];
            BOOL result = [currentVersion compare:lastVersion options:NSNumericSearch] == NSOrderedAscending;
            trackViewURL = [releaseInfo objectForKey:@"trackViewUrl"];
            if (result) {
               AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
                manager.responseSerializer = [AFHTTPResponseSerializer serializer];
                [manager GET:@"http://123.57.1.143/api/version/ios" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    NSString *version = responseObject;
                    if ([version isEqualToString:lastVersion]) {
                        //                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新" message:@"有新的版本更新可用，前往更新" delegate:self cancelButtonTitle:nil otherButtonTitles:@"更新", nil];
                        //                        alert.tag = 10141;
                        //                        [alert show];
                        [JCAlertView showOneButtonWithTitle:@"版本更新" Message:@"有新的版本更新可用, 前往更新" ButtonType: JCAlertViewButtonTypeDefault ButtonTitle:@"更新" Click:^{
                            NSString *string = @"itms-apps://itunes.apple.com/app/id991508808";
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
                            isReviewed = YES;
                            [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:isReviewed] forKey:@"isReviewed"];
                        }];
                    } else {
                        [JCAlertView showTwoButtonsWithTitle:@"版本更新" Message:@"有新版本更新可用, 前往更新" ButtonType: JCAlertViewButtonTypeCancel ButtonTitle:@"取消" Click:^{
                            
                        } ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"更新" Click:^{
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewURL]];
                        }];
                    }
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                    [JCAlertView showTwoButtonsWithTitle:@"版本更新" Message:@"有新版本更新可用, 前往更新" ButtonType: JCAlertViewButtonTypeCancel ButtonTitle:@"取消" Click:^{
                        
                    } ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"更新" Click:^{
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewURL]];
                    }];
                }];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (void)reviewApp{
    NSNumber *num = [[NSUserDefaults standardUserDefaults] valueForKey:@"lanuchCount"];
    if (!isReviewed && num.intValue == 4) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"请为声音实验室评分" ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"稍后提醒" Click:^{
                isReviewed = NO;
                [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:isReviewed] forKey:@"isReviewed"];
                [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:0] forKey:@"lanuchCount"];
            } ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"现在就去" Click:^{
                NSString *string = @"itms-apps://itunes.apple.com/app/id991508808";
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
                isReviewed = YES;
                [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:isReviewed] forKey:@"isReviewed"];
            }];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请为声音实验室评分" delegate:self cancelButtonTitle:@"现在就去" otherButtonTitles:@"稍后提醒", nil];
//            alert.tag = 10113;
//            [alert show];
        });
    }
}
#pragma mark -
- (void)createNavgationBar
{
    self.mostNewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _mostNewBtn.frame = CGRectMake(5, 0, 50, 30);
    [_mostNewBtn setTitle:@"最新" forState:UIControlStateNormal];
    [_mostNewBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
    _mostNewBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [_mostNewBtn addTarget:self action:@selector(mostNew:) forControlEvents:UIControlEventTouchUpInside];
    
//    self.plazzBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _plazzBtn.frame = CGRectMake(_mostNewBtn.right + 10, 0, 50, 30);
//    [_plazzBtn  setTitle:@"推荐" forState:UIControlStateNormal];
//    [_plazzBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    _plazzBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
//    [_plazzBtn addTarget:self action:@selector(plazz:) forControlEvents:UIControlEventTouchUpInside];
    
    self.attentionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _attentionBtn.frame = CGRectMake(_mostNewBtn.right + 10, 0, 50, 30);
    [_attentionBtn  setTitle:@"关注" forState:UIControlStateNormal];
    _attentionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [_attentionBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
    [_attentionBtn addTarget:self action:@selector(plazzAttention:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    [buttonView addSubview:_attentionBtn];
    [buttonView addSubview:_mostNewBtn];
//    [buttonView addSubview:_plazzBtn];
    self.navigationItem.titleView = buttonView;
    
    UIButton *seaechItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [seaechItem setImage:[UIImage imageNamed:@"search@3x"] forState:UIControlStateNormal];
    [seaechItem addTarget:self action:@selector(navigationSearchButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:seaechItem];
    self.navigationItem.rightBarButtonItem=rightItem;
    
    self.reconmend = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 20)];
    [_reconmend setImage:[UIImage imageNamed:@"reconmend"] forState:UIControlStateNormal];
    [_reconmend addTarget:self action:@selector(navigationReconmendButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_reconmend];
    self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)navigationReconmendButtonClick:(UIButton *)sender
{
    BOOL isSigned;
    if (sender) {
        isSigned = YES;
    } else {
        isSigned = NO;
    }
    reconmendAlertView *reconmendView = [[reconmendAlertView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height) isSigned:isSigned];
    [reconmendView.dismiss addTarget:self action:@selector(closeAlertAfterSign:) forControlEvents:UIControlEventTouchUpInside];
    reconmendView.playBtnBlock = ^(NSString *uid, NSString *workid){
        UINavigationController *nv = (UINavigationController *)self.mm_drawerController.rightDrawerViewController;
        self.playerCon = (playerTableViewController *)nv.topViewController;
        if ([_playerCon.header.streamer status] != DOUAudioStreamerPlaying || !_playerCon.header.streamer) {
            [self requestWorkInfo:workid isplayer:NO];
        }
    };
    reconmendView.stopBlock = ^(){
        [self playerStop];
    };
    CUSFlashLabel *label = [[CUSFlashLabel alloc] initWithFrame:CGRectMake(0, reconmendView.bottom - 64, self.view.width, 40)];
    label.backgroundColor = [UIColor clearColor];
    label.text = @"滑动查看更多";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont boldSystemFontOfSize:16];
    label.textAlignment = NSTextAlignmentCenter;
    [reconmendView addSubview:label];
    [label startAnimating];
    self.alert = [[JCAlertView alloc] initWithCustomView:reconmendView dismissWhenTouchedBackground:NO];
    
    [_alert show];
}

- (void)navigationSearchButtonClick:(UIButton *)sender
{
    search_viewController *searchVC = [[search_viewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (void)seaechClick:(NSNotification *)noti
{
    search_labelController *searchLabelVC = [[search_labelController alloc]init];
//    NSString *str = [NSString stringWithFormat:@"#%@#",noti.object];
    [searchLabelVC getTitle:noti.object];
    [self.navigationController pushViewController:searchLabelVC animated:YES];
}
- (void)plazz:(UIButton *)sender
{
    [_BigScroll setContentOffset:CGPointMake(self.view.frame.size.width, 0) animated:YES];
    [_plazzBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _plazzBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [_mostNewBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
    [_attentionBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
    _mostNewBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    _attentionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
}
- (void)mostNew:(UIButton *)sender
{
    [_BigScroll setContentOffset:CGPointZero animated:YES];
    [_mostNewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _mostNewBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [_plazzBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
    [_attentionBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
    _plazzBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    _attentionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
}
- (void)plazzAttention:(UIButton *)sender
{
    [_BigScroll setContentOffset:CGPointMake(self.view.frame.size.width, 0) animated:YES];
    [_attentionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _attentionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [_plazzBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
    [_mostNewBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
    _plazzBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    _mostNewBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
}
#pragma mark -
- (void)createTableview
{
    self.BigScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -64 - 48)];
    _BigScroll.contentSize = CGSizeMake(2*self.view.frame.size.width, 0);
    _BigScroll.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    _BigScroll.backgroundColor = [UIColor whiteColor];
    _BigScroll.delegate = self;
    _BigScroll.showsHorizontalScrollIndicator = NO;
    _BigScroll.pagingEnabled = YES;
    [self.view addSubview:_BigScroll];
    //推荐
//    self.plazzTableview = [[PlazzTableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, _BigScroll.frame.size.height)];
//    [_BigScroll addSubview:_plazzTableview];
    //最新
    self.mostNewTableview = [[MostNewTableview alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, _BigScroll.frame.size.height)];
    [_BigScroll addSubview:_mostNewTableview];
    //关注
    self.attentionTableview = [[AttentionTableview alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, _BigScroll.frame.size.height)];
    [_BigScroll addSubview:_attentionTableview];
    
//    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"uid"] == nil) {
        [self mostNew:_mostNewBtn];
//    } else {
//        [self mostNew:_mostNewBtn];
//    }
}
#pragma mark -
- (void)plazzgotoPlayer:(NSNotification *)notification
{
    [self closeSignAlert];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *isPlay = [userDefaults objectForKey:@"isWiFiPlay"];
    NSString *pppp=[NSString stringWithFormat:@"%u",[self getNetworkTypeFromStatusBar]];
    if (([isPlay isEqualToString:@"1"]&&[pppp isEqualToString:@"5"])||[pppp isEqualToString:@"5"]||([isPlay isEqualToString:@"0"]&&![pppp isEqualToString:@"0"])||isPlay==nil){
        [self requestWorkInfo:notification.object isplayer:YES];
    } else {
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"你已开启仅Wifi环境播放" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            set_ViewController *setVC = [[set_ViewController alloc]init];
            [self.navigationController pushViewController:setVC animated:YES];
        }];
    }
}
- (void)requestWorkInfo:(NSString *)workid isplayer:(BOOL)isplayer
{
    UINavigationController *nv = (UINavigationController *)self.mm_drawerController.rightDrawerViewController;
//    self.player = (PlayerViewController *)nv.topViewController;
    self.playerCon = (playerTableViewController *)nv.topViewController;
    NSString *CinemeID = workid;
    NSString *showURL = [NSString stringWithFormat:@"http://123.57.1.143/api/work/%@/show", workid];
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSString *head = [NSString stringWithFormat:@"Token %@:%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"uid"], [[NSUserDefaults standardUserDefaults] valueForKey:@"token"]];
    [manager.requestSerializer setValue:head forHTTPHeaderField:@"authorization"];
    [manager GET:showURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self fetchWorkInfo:responseObject workid:CinemeID isPlayerShow:isplayer];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"该作品已被作者删除" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];
        
    }];
}
- (void)fetchWorkInfo:(id)responseObject workid:(NSString *)workid isPlayerShow:(BOOL)isPlayerShow
{
    Player_ShowModel *playModel = [Player_ShowModel sharedInstance];
    
    NSDictionary *dic = (NSDictionary *)responseObject;
    NSDictionary *dataDic = [dic valueForKey:@"data"];
    NSDictionary *linksDic = [dic valueForKey:@"links"];
    [playModel setValuesForKeysWithDictionary:dataDic];
    if ([[dic allKeys] containsObject:@"meta"]) {
        NSDictionary *meta = [dic valueForKey:@"meta"];
        NSString *userAvatar = [meta valueForKey:@"userAvatar"];
        _playerCon.locUserAvatar = userAvatar;
//        NSArray *array = [meta valueForKey:@"playlist"];
//        _player.playList = [NSMutableArray arrayWithArray:array];
        playModel.comment_count = [meta valueForKey:@"count"];
//        ;
    }
    _playerCon.waveform = [linksDic valueForKey:@"waveform"];
    _playerCon.isSelf = NO;
//    if (![_player.workID isEqualToString:workid]) {
        _playerCon.workID = workid;
        [_playerCon reloadViews:playModel];
//    }
    if (isPlayerShow == YES) {
        [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:^(BOOL finished) {
        }];
    } else {
        
    }
}
- (void)playerStop
{
    UINavigationController *nv = (UINavigationController *)self.mm_drawerController.rightDrawerViewController;
    self.playerCon = (playerTableViewController *)nv.topViewController;
//    if (_player.streamer.status == DOUAudioStreamerPlaying) {

//        [_player stopAudioPlayer];
//    }
}
-(NETWORK_state)getNetworkTypeFromStatusBar {
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
    NSNumber *dataNetworkItemView = nil;
    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]])     {
            dataNetworkItemView = subview;
            break;
        }
    }
    NETWORK_state nettype = NETWORK_state_NONE;
    NSNumber * num = [dataNetworkItemView valueForKey:@"dataNetworkType"];
    nettype = [num intValue];
    return nettype;
}
- (void)plazzgoToUser:(NSNotification *)notification
{
    NSString *userId = notification.object;
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    //读取字典类型NSDictionary类型的数据
    NSString *uid = [userDefaultes stringForKey:@"uid"];
    if (![userId isEqualToString: uid] ) {
        userInformationViewController *otherSelfVC = [[userInformationViewController alloc]initWithSelfUid:userId];
        [self.navigationController pushViewController:otherSelfVC animated:YES];
    }else if([userId isEqualToString: uid]){
        //自动跳到我的
        if (self.tabBarController.selectedIndex == 4) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        } else {
            [self.tabBarController setSelectedIndex:4];
        }
    }
}

#pragma mark -
#pragma mark ScrollView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == CGPointZero.x) {
        [_mostNewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _mostNewBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
//        [_plazzBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
        [_attentionBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
//        _plazzBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _attentionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    } else if (scrollView.contentOffset.x == self.view.frame.size.width) {
//        [_plazzBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        _plazzBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
//        [_mostNewBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
//        [_attentionBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
//        _mostNewBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
//        _attentionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        if ([[NSUserDefaults standardUserDefaults] valueForKey:@"uid"] == nil) {
            //我的
            //提示请先登陆
            [JCAlertView showTwoButtonsWithTitle:@"未登录" Message:@"登录后才能继续操作" ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"再看看" Click:^{
                [self plazz:_plazzBtn];
            } ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"去登录" Click:^{
                [[AppDelegate shareDelegate] chooseRootViewController];
            }];
        } else {
            [_attentionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            _attentionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
//            [_plazzBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
            [_mostNewBtn setTitleColor:[UIColor colorWithWhite:0.405 alpha:1.000] forState:UIControlStateNormal];
//            _plazzBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            _mostNewBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        }

    } else if (scrollView.contentOffset.x == self.view.frame.size.width * 2) {
        
    }
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    offset = scrollView.contentOffset.x;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    offset = scrollView.contentOffset.x;
}
#pragma mark
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag==80001) {
        if (buttonIndex == 0) {
        }else if (buttonIndex == 1){
            //跳到设置
            set_ViewController *setVC = [[set_ViewController alloc]init];
            [self.navigationController pushViewController:setVC animated:YES];
        }
    }else if (alertView.tag==12345){
        if (buttonIndex == 1){
            //跳到登陆页
            [[AppDelegate shareDelegate] chooseRootViewController];
        } else {
//            [self plazz:_plazzBtn];
        }
    } else if (alertView.tag == 10112) {
        if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewURL]];
        }
    } else if (alertView.tag == 10113) {
        if (buttonIndex == 0) {
            NSString *string = @"itms-apps://itunes.apple.com/app/id991508808";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
            isReviewed = YES;
            [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:isReviewed] forKey:@"isReviewed"];
        } else {
            isReviewed = NO;
            [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:isReviewed] forKey:@"isReviewed"];
            [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:0] forKey:@"lanuchCount"];
        }
    } else if (alertView.tag == 10141) {
        if (buttonIndex == 0) {
            NSString *string = @"itms-apps://itunes.apple.com/app/id991508808";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
            isReviewed = YES;
            [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:isReviewed] forKey:@"isReviewed"];
        }
    } else {
        if (buttonIndex == 0) {
            cover.alpha = 0;
            cover = nil;
        }else if (buttonIndex == 1){
            
            cover.alpha = 0;
            cover =nil;
        }
    }    
}
- (void)loadingAnimation
{
    [_loadingImage removeFromSuperview];
    _loadingImage = nil;
    
    [cover removeFromSuperview];
    cover = nil;
    
    [UIView animateWithDuration:0.5 animations:^{
        cover = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
        cover.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:cover];
        _loadingImage = [[UIImageView alloc] initWithFrame:CGRectMake(kDeviceWidth / 2 - 80, kDeviceHeight / 2 - 150, 160, 160)];
        _loadingImage.backgroundColor = [UIColor clearColor];
        NSArray *animation = [NSArray arrayWithObjects:[UIImage imageNamed:@"loadingAnimationblack1"],[UIImage imageNamed:@"loadingAnimationblack2"], [UIImage imageNamed:@"loadingAnimationblack3"],[UIImage imageNamed:@"loadingAnimationblack4"],[UIImage imageNamed:@"loadingAnimationblack5"],[UIImage imageNamed:@"loadingAnimationblack6"], [UIImage imageNamed:@"loadingAnimationblack7"],[UIImage imageNamed:@"loadingAnimationblack8"],[UIImage imageNamed:@"loadingAnimationblack9"],[UIImage imageNamed:@"loadingAnimationblack10"],[UIImage imageNamed:@"loadingAnimationblack11"],nil];
        _loadingImage.animationImages = animation;
        _loadingImage.animationDuration = 0.4;
        _loadingImage.animationRepeatCount = 2;
        //        _loadingImage.image = [UIImage imageNamed:@"loadingAnimation3"];
        [_loadingImage startAnimating];
        [cover addSubview:_loadingImage];
        
    }completion:^(BOOL finished) {
                [_loadingImage removeFromSuperview];
                _loadingImage = nil;
        
                [cover removeFromSuperview];
                cover = nil;
        self.navigationController.navigationBar.translucent = NO;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
